<template>
  <div>
    <div class="box" @click="gotop" v-show="shows" >
      <van-icon name="arrow-up" />
      <p>回到顶部</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      top: 0,
      shows: false,
      timer: null,
    };
  },
  mounted() {
    window.onscroll = () => {
      this.top = window.document.scrollingElement.scrollTop;
      this.shows =this.top > 1000;
      console.log( this.shows);
    };
  },
  methods: {
    gotop() {
      this.timer = setInterval(() => {
        if (this.top == 0) clearInterval(this.timer);
        let top = this.top;
        top -= 50;
        window.document.scrollingElement.scrollTo(top, top);
      }, 5);
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  p {
    padding: 0;
    margin: 0;
  }
  flex-direction: column;
  text-align: center;
  align-items: center;
  font-size: 0.3rem;
  position: fixed;
  right: 3rem;
  bottom: 5rem;
  width: 3.5rem;
  height: 3.5rem;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  background-color: rgba(#e74c3c, 0.8);
}
</style>